import common.*;

import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.net.FileReference;
import flash.net.URLStream;

import mx.controls.Alert;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;

[Embed(source="images/error.png")]private var errorIcon:Class;
[Embed(source="images/success.png")]private var successIcon:Class;
[Embed(source="images/reset.png")]private var resetIcon:Class;
[Embed(source="images/question.png")]private var questionIcon:Class;

private var uploadUrl:String;
private var downloadUrl:String;
public var uploadFileref:FileReference;
public var downloadFileRef:FileReference;

[Bindable]public var fileName:String="";
[Bindable]public var urlStream:URLStream;
[Bindable]public var filePath:String;
[Bindable]public var filePath2:String;

[Bindable]public var urlPrefix:String;
[Bindable]public var params:Object={};
[Bindable]public var entityXml:XML;
[Bindable]public var programXml:XML;
[Bindable]public var branchXml:XML;
[Bindable]public var specializationXml:XML;
[Bindable]public var semesterXml:XML;

public function onPageCreate():void
{
	urlPrefix=commonFunction.getConstants('url')+"/prestagingData/";
	uploadUrl=commonFunction.getConstants('url')+"/fileUpload/upload.jsp?fileName=";
	downloadUrl=commonFunction.getConstants('url')+commonFunction.getConstants('prestagingTemplateUrl')+"/PrestagingDataSheetFor";
	
	filePath=commonFunction.getConstants('url')+"/PrestagingDataDocuments/";
	filePath2=commonFunction.getConstants('url')+"/UploadedPrestagingDataDocuments/";
	params['time']=new Date;
	getEntityList.send(params);
}

public function onStudentTypeChange(event:Event):void
{
	downloadButton.enabled=false;
    urlStream=new URLStream;
	urlStream.addEventListener(Event.COMPLETE, onAvailable);
	urlStream.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
	urlStream.load(new URLRequest(downloadUrl+studentType.selectedLabel+".xls"));
}

public function onAvailable(e:Event):void
{
    urlStream.close();
    downloadButton.enabled=true;
}

public function onIOError(ev:IOErrorEvent):void
{
	urlStream.close();
    Alert.show("File missing for this type of student",commonFunction.getMessages('error'),0,null,null,errorIcon);
}

public function onDownload():void
{
	downloadFileRef=new FileReference;
	downloadFileRef.addEventListener(Event.CANCEL,onCancel);
	downloadFileRef.addEventListener(Event.CLOSE,onCancel);
	downloadFileRef.addEventListener(Event.COMPLETE,onCancel);
	downloadFileRef.download(new URLRequest(downloadUrl+studentType.selectedLabel+".xls"),"PrestagingDataTemplateFor"+studentType.selectedLabel+".xls");
}

public function onCancel(event:Event):void
{
	studentType.selectedIndex=-1;
	downloadButton.enabled=false;
}

public function getEntitySuccess(event:ResultEvent):void
{
	if(event.result.sessionConfirm == true)
	{
		Alert.show(resourceManager.getString('Messages','sessionInactive'),commonFunction.getMessages('error'),0,null,null,errorIcon);
	   	this.parentDocument.vStack.selectedIndex=0;
	   	this.parentDocument.loaderCanvas.removeAllChildren();
	}
	entityXml=event.result as XML;
	entityCombo.dataProvider=entityXml.Details.name;
}

public function onEntityChange(event:Event):void
{
	params['time']=new Date;
	params['entityId']=entityXml.Details.(name==entityCombo.selectedLabel).id;
	getProgramList.send(params);
	onReset(event);
	branchCombo.selectedIndex=-1;
	specilizationCombo.selectedIndex=-1;
	semesterCombo.selectedIndex=-1;
	startDateField.text="";
	endDateField.text="";
	branchCombo.enabled=false;
	specilizationCombo.enabled=false;
	semesterCombo.enabled=false;
	startDateField.enabled=false;
	endDateField.enabled=false;
	uploadPanel.visible=false;
}

public function getProgramSuccess(event:ResultEvent):void
{
	if(event.result.sessionConfirm == true)
	{
		Alert.show(resourceManager.getString('Messages','sessionInactive'),commonFunction.getMessages('error'),0,null,null,errorIcon);
	   	this.parentDocument.vStack.selectedIndex=0;
	   	this.parentDocument.loaderCanvas.removeAllChildren();
	}
	programXml=event.result as XML;
	programCombo.dataProvider=programXml.Details.name;
	programCombo.selectedIndex=-1;
	programCombo.enabled=true;
}

public function onProgramChange(event:Event):void
{
	params['time']=new Date;
	params['entityId']=entityXml.Details.(name==entityCombo.selectedLabel).id;
	params['programId']=programXml.Details.(name==programCombo.selectedLabel).id;
	getBranchList.send(params);
	onReset(event);
	specilizationCombo.selectedIndex=-1;
	semesterCombo.selectedIndex=-1;
	startDateField.text="";
	endDateField.text="";
	specilizationCombo.enabled=false;
	semesterCombo.enabled=false;
	startDateField.enabled=false;
	endDateField.enabled=false;
	uploadPanel.visible=false;
}

public function getBranchSuccess(event:ResultEvent):void
{
	if(event.result.sessionConfirm == true)
	{
		Alert.show(resourceManager.getString('Messages','sessionInactive'),commonFunction.getMessages('error'),0,null,null,errorIcon);
	   	this.parentDocument.vStack.selectedIndex=0;
	   	this.parentDocument.loaderCanvas.removeAllChildren();
	}
	branchXml=event.result as XML;
	branchCombo.dataProvider=branchXml.Details.name;
	branchCombo.selectedIndex=-1;
	branchCombo.enabled=true;
}

public function onBranchChange(event:Event):void
{
	params['time']=new Date;
	params['entityId']=entityXml.Details.(name==entityCombo.selectedLabel).id;
	params['programId']=programXml.Details.(name==programCombo.selectedLabel).id;
	params['branchId']=branchXml.Details.(name==branchCombo.selectedLabel).id;
	getSpecializationList.send(params);
	onReset(event);
	semesterCombo.selectedIndex=-1;
	startDateField.text="";
	endDateField.text="";
	semesterCombo.enabled=false;
	startDateField.enabled=false;
	endDateField.enabled=false;
	uploadPanel.visible=false;
}

public function getSpecializationSuccess(event:ResultEvent):void
{
	if(event.result.sessionConfirm == true)
	{
		Alert.show(resourceManager.getString('Messages','sessionInactive'),commonFunction.getMessages('error'),0,null,null,errorIcon);
	   	this.parentDocument.vStack.selectedIndex=0;
	   	this.parentDocument.loaderCanvas.removeAllChildren();
	}
	specializationXml=event.result as XML;
	specilizationCombo.dataProvider=specializationXml.Details.name;
	specilizationCombo.selectedIndex=-1;
	specilizationCombo.enabled=true;
}

public function onSpecializationChange(event:Event):void
{
	params['time']=new Date;
	params['programId']=programXml.Details.(name==programCombo.selectedLabel).id;
	params['branchId']=branchXml.Details.(name==branchCombo.selectedLabel).id;
	params['specializationId']=specializationXml.Details.(name==specilizationCombo.selectedLabel).id;
	getSemesterList.send(params);
	onReset(event);
	startDateField.text="";
	endDateField.text="";
	startDateField.enabled=false;
	endDateField.enabled=false;
	uploadPanel.visible=false;
}

public function getSemesterSuccess(event:ResultEvent):void
{
	if(event.result.sessionConfirm == true)
	{
		Alert.show(resourceManager.getString('Messages','sessionInactive'),commonFunction.getMessages('error'),0,null,null,errorIcon);
	   	this.parentDocument.vStack.selectedIndex=0;
	   	this.parentDocument.loaderCanvas.removeAllChildren();
	}
	semesterXml=event.result as XML;
	semesterCombo.dataProvider=semesterXml.role.description;
	semesterCombo.selectedIndex=-1;
	semesterCombo.enabled=true;
}

public function onFailure(event:FaultEvent):void
{
	Alert.show(commonFunction.getMessages('error'),commonFunction.getMessages('error'),0,null,null,errorIcon);  	
}

public function onSemesterChange(event:Event):void
{
	var ev:Event;
	onReset(ev);
	startDateField.text="";
	endDateField.text="";
	startDateField.enabled=true;
	endDateField.enabled=false;
	uploadPanel.visible=false;
}

public function onStartDateChange():void
{
	var ev:Event;
	onReset(ev);
	endDateField.text="";
	endDateField.enabled=true;
	uploadPanel.visible=false;
}

public function onEndDateChange():void
{
	fileName=entityXml.Details.(name==entityCombo.selectedLabel).id;
	fileName+="_"+programXml.Details.(name==programCombo.selectedLabel).id;
	fileName+="_"+branchXml.Details.(name==branchCombo.selectedLabel).id;
	fileName+="_"+specializationXml.Details.(name==specilizationCombo.selectedLabel).id;
	fileName+="_"+semesterXml.role.(description==semesterCombo.selectedLabel).id;
	fileName+="_"+startDateField.text;
	fileName+="_"+endDateField.text+"_prestagingData.xls";
	
	urlStream=new URLStream;
	urlStream.addEventListener(Event.COMPLETE, onFound);
	urlStream.addEventListener(IOErrorEvent.IO_ERROR, onNotFound);
	urlStream.load(new URLRequest(filePath+fileName));
}

public function onFound(e:Event):void
{
    urlStream.close();
    uploadPanel.visible=false;
    Alert.show("File Already Uploaded For This Combination");
	onReset(e);
}

public function onFinalFound(e:Event):void
{
    urlStream.close();
    uploadPanel.visible=false;
    Alert.show("File And Data Already Uploaded For This Combination");
	onReset(e);
}

public function onNotFound(event:IOErrorEvent):void
{
	urlStream=new URLStream;
	urlStream.addEventListener(Event.COMPLETE, onFinalFound);
	urlStream.addEventListener(IOErrorEvent.IO_ERROR, onFinalNotFound);
	urlStream.load(new URLRequest(filePath2+fileName));
}

public function onFinalNotFound(event:IOErrorEvent):void
{
	urlStream.close();
    uploadPanel.visible=true;
    var e:Event;
	onReset(e);
}

public function onBrowse(event:Event):void
{
	uploadFileref=new FileReference;
	uploadFileref.addEventListener(Event.SELECT,onFileSelect);
	uploadFileref.addEventListener(ProgressEvent.PROGRESS,showProgress);
	uploadFileref.browse([new FileFilter("Excel File(.xls,.xlsx)","*.xls;*.xlsx")]);
}

public function onFileSelect(event:Event):void
{
	uploadFileref.addEventListener(Event.COMPLETE,onLoadComplete);
	uploadFileref.addEventListener(Event.CANCEL,onReset);
	uploadFileref.load();
}

public function onLoadComplete(event:Event):void
{
	fileLabel.text=uploadFileref.name;
	resetButton.enabled=true;
	uploadButton.enabled=true;
	browseButton.enabled=false;
}

public function showProgress(event:ProgressEvent):void
{
	uploadFileName.setProgress(event.bytesLoaded, event.bytesTotal);
}

public function onReset(event:Event):void
{
	resetButton.enabled=false;
	uploadButton.enabled=false;
	browseButton.enabled=true;
	uploadFileName.setProgress(0,100);
	uploadFileref=null;
	fileLabel.text=commonFunction.getConstants('selectExcelToUpload');
}

public function onUpload(event:Event):void
{
	uploadFileref.upload(new URLRequest(uploadUrl+fileName));
	Alert.show(commonFunction.getMessages('successOnUpload'),commonFunction.getMessages('success'),0,null,null,successIcon);
	onReset(event);
	uploadPanel.visible=false;
	entityCombo.selectedIndex=-1;
	programCombo.selectedIndex=-1;
	branchCombo.selectedIndex=-1;
	specilizationCombo.selectedIndex=-1;
	semesterCombo.selectedIndex=-1;
}